package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.hs.panghu.dao.CommunityDao;
import com.hs.panghu.dao.impl.CommunityDaoImpl;
import com.hs.panghu.entity.Community;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        // 连接数据库
        CommunityDao communityDao = new CommunityDaoImpl();
        if ("communityList".equals(action)) {  // 分页查询
            Map<String, String> map = new HashMap<>();
            String name = req.getParameter("name");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            map.put("name", name);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);


            // 动态获取社区集合信息
            List<Community> list = communityDao.selectCommunity(map);
            // 动态获取社区的数据总条数
            int total = communityDao.selectCommunityCount(map);
            // 创建一个返回值的map集合，用来包裹list数据和total总数量
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            //把结果集合，封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("communityUpdate".equals(action)) { //添加或修改
            String flag = req.getParameter("addOrUpdateFlag");
            String communityAddress = req.getParameter("communityAddress");
            String communityAreaCode = req.getParameter("communityAreaCode");
            if ("".equals(communityAreaCode)) {
                communityAreaCode = "0";
            }
            String communityCity = req.getParameter("communityCity");
            String communityCoordinates = req.getParameter("communityCoordinates");
            String communityDistrict = req.getParameter("communityDistrict");
            String communityId = req.getParameter("communityId");
            if ("".equals(communityId)) {
                communityId = "0";
            }
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
            String communityShopCount = req.getParameter("communityShopCount");
            String communityStatus = req.getParameter("communityStatus");
            String communityUserCount = req.getParameter("communityUserCount");
//            String createTime = req.getParameter("createTime");
            String distance = req.getParameter("distance");

            if("1".equals(flag)){//添加
                //组成对象
                Community community = new Community(
                        Integer.parseInt(communityId),
                        communityName,
                        communityProvince,
                        communityAddress,
                        communityCity,
                        communityDistrict,
                        communityLatitude,
                        communityLongitude,
                        Integer.parseInt(communityAreaCode),
                        communityCoordinates
                );
                int row = communityDao.insertCommunity(community);
                if(row!=0){
                    Result success = Result.success();
                    resp.getWriter().write(JSON.toJSONString(success));
                }else {
                    Result error = Result.error();
                    resp.getWriter().write(JSON.toJSONString(error));
                }
            }
            if("2".equals(flag)){//修改
                Community community = new Community(
                        Integer.parseInt(communityId),
                        communityName,
                        communityProvince,
                        communityCity,
                        communityDistrict,
                        communityAddress,
                        communityLatitude,
                        communityLongitude,
                        Integer.parseInt(communityShopCount),
                        Integer.parseInt(communityAreaCode),
                        communityCoordinates,
                        Integer.parseInt(communityStatus),
                        Integer.parseInt(communityUserCount),
                        distance
                );
                int row = communityDao.updateCommunity(community);
                if(row!=0){
                    Result success = Result.success();
                    resp.getWriter().write(JSON.toJSONString(success));
                }else {
                    Result error = Result.error();
                    resp.getWriter().write(JSON.toJSONString(error));
                }
            }

        } else if ("communityDelete".equals(action)) {
            String communityId = req.getParameter("communityId");
            if(communityId==null || "".equals(communityId)){
                communityId = "0";
            }
            int row = communityDao.deleteCommunity(Integer.valueOf(communityId));
            if(row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }else if("communityCount".equals(action)){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            Map<String, String> map = new HashMap<>();
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            // 动态获取社区集合信息
            List<Community> list = communityDao.selectCommunity(map);
            // 动态获取社区的数据总条数
            int total = communityDao.selectCommunityCount(map);
            // 创建一个返回值的map集合，用来包裹list数据和total总数量
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            //把结果集合，封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}